<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Models\AppleTree;
use Encore\Admin\Controllers\AdminController;

class HipsterAppleTreesController extends AdminController
{
    protected $title = '变异苹果树';

    protected function grid()
    {
        $grid = new Grid(new AppleTree());
        $grid->model()->orderBy('id');

        $grid->column('id', 'ID');
        $grid->column('title', '标题');
        $grid->column('apple_tree_period', '苹果树有效期');
        $grid->column('consume_apples', '消耗苹果');
        $grid->column('produce_apples', '产生苹果');
        $grid->column('buy_limit', '限制数量');
        $grid->column('image', '图片')->lightbox(['width' => 50, 'height' => 50]);
        

        $grid->disableRowSelector();
        $grid->disableColumnSelector();
        $grid->disableExport();
        $this->gridActions($grid);

        return $grid;
    }

	protected function gridActions(Grid $grid)
	{
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->disableDelete();
		});
	}

    protected function form()
    {
        $form = new Form(new AppleTree());

        $form->text('title', '标题')->rules('required');
        $form->number('apple_tree_period', '苹果树有效期')->default(30)->rules('required|min:0|max:300');
         $form->number('buy_limit', '限制数量')->default(10)->rules('required|min:0');
        $form->currency('consume_apples', '消耗苹果')->symbol('￥')->default(100.00)->rules('required|min:0');
	    $form->currency('produce_apples', '产生苹果')->symbol('￥')->default(120.00)->rules('required|min:0');
	    $form->image('image', '图片')->uniqueName();

        $this->formTools($form);
        $this->formSaving($form);

        return $form;
    }

	protected function formTools(Form $form)
	{
		$form->tools(function (Form\Tools $tools) {
			$tools->disableView();
			$tools->disableDelete();
		});
	}

	protected function formSaving(Form $form)
	{
		$form->saving(function (Form $form) {
			/*if ($form->isEditing() and ($form->model()->is_locking)) {
				admin_error('当前场次正在金店金店活动中...不可编辑!');
				return redirect('admin/gold-rounds');
			}*/
			return true;
		});
	}
}
